﻿#region << 版 本 注 释 >>
/*----------------------------------------------------------------
 * 版权所有 (c) 2022 北京超维景生物科技有限公司 保留所有权利。
 * 
 * 创建者：huangyang
 * 电子邮箱：huangyang@tvscope.cn
 * 创建时间：2023/3/13 16:53:11
 * 版本：V1.0.0
 * 描述：
 *
 * ----------------------------------------------------------------
 * 修改人：
 * 时间：
 * 修改说明：
 *
 * 版本：V1.0.1
 *----------------------------------------------------------------*/
#endregion << 版 本 注 释 >>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ImageK.Gui;
using ImageK.Process;

namespace ImageK.Plugin.Filter
{
	/** This plugin implements the Invert, Smooth, Sharpen, Find Edges, 
	and Add Noise commands. */
    public class Filters : BasePlugInFilter
    {
        private static double sd = Prefs.getDouble(Prefs.NOISE_SD, 25.0);
        private string arg;
        private ImagePlus imp;
        private int slice;
        private bool canceled;
        private bool noRoi;

        public override int setup(string arg, ImagePlus imp)
        {
            this.arg = arg;
            this.imp = imp;
            if (imp != null)
            {
                Roi roi = imp.getRoi();
                if (roi != null && !roi.isArea())
                    noRoi = true;
            }
            int flags = IJ.setupDialog(imp, DOES_ALL - DOES_8C + SUPPORTS_MASKING);
            return flags;
        }

        public override void run(ImageProcessor ip)
        {

            if (noRoi)
                ip.resetRoi();

            if (arg.Equals("invert"))
            {
                ip.invert();
                slice++;
                if (imp.getBitDepth() == 16 && imp.getStackSize() > 1 && slice == imp.getStackSize())
                    imp.resetDisplayRange();
                return;
            }

            if (arg.Equals("smooth"))
            {
                ip.setSnapshotCopyMode(true);
                ip.smooth();
                ip.setSnapshotCopyMode(false);
                return;
            }

            if (arg.Equals("sharpen"))
            {
                ip.setSnapshotCopyMode(true);
                ip.sharpen();
                ip.setSnapshotCopyMode(false);
                return;
            }

            if (arg.Equals("edge"))
            {
                ip.setSnapshotCopyMode(true);
                ip.findEdges();
                ip.setSnapshotCopyMode(false);
                return;
            }

            if (arg.Equals("add"))
            {
                ip.noise(25.0);
                return;
            }

            if (arg.Equals("noise"))
            {
                if (canceled)
                    return;
                slice++;
                if (slice == 1)
                {
                    GenericDialog gd = new GenericDialog("Gaussian Noise");
                    gd.addNumericField("Standard Deviation:", sd, 2);
                    gd.showDialog();
                    if (gd.wasCanceled())
                    {
                        canceled = true;
                        return;
                    }
                    sd = gd.getNextNumber();
                }
                ip.noise(sd);
                IJ.register(typeof(Filters));
                return;
            }
        	 	    
        }
		    
        /** Returns the default standard deviation used by Process/Noise/Add Specified Noise. */
        public static double getSD()
        {
            return sd;
        }
	
    }

}
